from ..testLogin.LoginOperation import LoginOperation
from ..PublicOperations import PublicOperations
from selenium.webdriver.common.by import By
import time


class BankCardOperation:

    # 先登录
    def bankCardOperate(self, driver, username, pwd, bankName, bankNumber, arg1):

        LoginOperation().login(driver, username, pwd)
        PublicOperations.Operate(driver, 4, 3)
        driver.find_element(By.XPATH, '//button[@id="bankCard_add_modal_btn"]').click()
        # 新增按钮
        time.sleep(1)
        driver.find_element(By.XPATH, '//input[@id="cardBank_add_input"]').send_keys(bankName)

        if arg1 == 1:
            driver.find_element(By.XPATH,
                                "//div[@id='bankCardAddModal']//div[@class='modal-body']/form/div[2]/div[1]/div/label[2]/span").click()
        # arg1 1 银行卡 类型为信用卡

        driver.find_element(By.XPATH, '//input[@id="cardNum_add_input"]').send_keys(bankNumber)
        driver.find_element(By.XPATH, '//button[@id="bankCard_save_btn"]').click()
        time.sleep(2)

    # 获取银行卡数量
    def __getBankNum(self, driver):
        return driver.find_elements(By.XPATH, "//div[@class='row']/div[not(@class)]/div")

    def __operate(self, driver, args):
        driver.find_element(By.XPATH, f"//div[@class='jconfirm-buttons']/button[{args}]").click()

    # 删除银行卡 操作
    def delBankCard(self, driver, username, pwd, arg1):
        LoginOperation().login(driver, username, pwd)
        PublicOperations.Operate(driver, 4, 3)
        cards = self.__getBankNum(driver)
        cards[0].find_element(By.XPATH,
                              './div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"delete_btn")]').click()
        #  对第一张银行卡 执行 删除操作
        time.sleep(1)
        if arg1 == 1:
            self.__operate(driver, 1)
            # 确认
        else:
            self.__operate(driver, 2)
            # 执行 取消
        time.sleep(1)
        # 确认删除按钮

    def __operate2(self, driver, newValue, arg1):
        time.sleep(1)
        ans = ''
        if arg1 == 1 or arg1 == 2:
            ele = None
            if arg1 == 1:
                ele = driver.find_element(By.XPATH, '//input[@id="cardBank_update_input"]')
                # 银行卡名
            elif arg1 == 2:
                # 银行卡号
                ele = driver.find_element(By.XPATH, '//input[@id="cardNum_update_input"]')
            ans = ele.get_attribute("value")
            ele.clear()
            ele.send_keys(newValue)
        elif arg1 == 3:
            driver.find_element(By.XPATH,
                                '//div[@id="bankCardUpdateModal"]/div//form/div[2]/div[1]/div/label[2]/span').click()
            # 选择信用卡

        driver.find_element(By.XPATH, '//button[@id="bankCard_update_btn"]').click()
        # 执行更新按钮

        return ans

    # 更新银行卡数据
    def updateBankCard(self, driver, username, pwd, newVal, arg1):
        LoginOperation().login(driver, username, pwd)
        PublicOperations.Operate(driver, 4, 3)
        cards = self.__getBankNum(driver)
        cards[0].find_element(By.XPATH,
                              './div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"update_btn")]').click()
        # 对第一张银行卡 执行 编辑操作

        ans = self.__operate2(driver, newVal, arg1)
        time.sleep(1)
        return ans

    def getSuccessData(self, driver):
        cards = self.__getBankNum(driver)
        # print(cards[-1])
        result = cards[-1].find_element(By.XPATH, "./div/div[@class='card-body']/h4[2]").text

        return result[result.find('：') + 1:result.find('元')]

    def getModifyBankName(self,driver):
        time.sleep(2)
        cards = self.__getBankNum(driver)

        result = cards[0].find_element(By.XPATH,"./div/div[1]/div/h4").text

        return result[:result.find('(')].strip()


    def getModifyBankNumber(self, driver):
        time.sleep(2)
        cards = self.__getBankNum(driver)

        result = cards[0].find_element(By.XPATH, "./div/div[2]/h4[1]").text

        return result[result.find('：')+1:].strip()

    # 卡号：1234567891548796

    # 中国人民银行 (借记卡)
    def getBankCardType(self,driver):
        time.sleep(2)
        cards = self.__getBankNum(driver)

        result = cards[0].find_element(By.XPATH, "./div/div[1]/div/h4").text

        return result[result.rfind('(') + 1:result.rfind(')')]
